<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_prosedur extends CI_Model {
    
	public function __construct(){
        parent::__construct();
    }
	
	/* public public function option_departemen($d=""){
		$value = isset($d['value'])?$d['value']:'';
		$out = '';
		
		$this->db->flush_cache();
		$this->db->from('tbl_ref_departemen');
		$this->db->order_by('kd_departemen');
		
		$res = $this->db->get();
		
		$out = '<option value="">-- Pilih --</option>';
		foreach($res->result() as $r){
			$out .= '<option value="'.$r->kd_departemen.'" '.($r->kd_departemen==$value?'selected="selected"':'').'>'.$r->nama_departemen.'</option>';
		}
		
		return $out;
	} */
	
	public function option_ttd($d=""){
		$value = isset($d['value'])?$d['value']:'';
		$out = '';
		
		$this->db->flush_cache();
		$this->db->from('ttd');
		$this->db->where('status','A');
		$this->db->order_by('id_ttd');
		
		$res = $this->db->get();
		
		$out = '<option value="">-- Pilih --</option>';
		foreach($res->result() as $r){
			$out .= '<option value="'.$r->id_ttd.'" '.($r->id_ttd==$value?'selected="selected"':'').'>'.$r->id_ttd.'</option>';
		}
		
		return $out;
	}

	public function option_desa($d=""){
		$value = isset($d['value'])?$d['value']:'';
		$out = '';
		
		$this->db->flush_cache();
		$this->db->from('desa');
		$this->db->where('status','A');
		$this->db->order_by('id_desa');
		
		$res = $this->db->get();
		
		$out = '<option value="">-- Pilih --</option>';
		foreach($res->result() as $r){
			$out .= '<option value="'.$r->id_desa.'" '.($r->id_desa==$value?'selected="selected"':'').'>'.$r->nama_desa.'</option>';
		}
		
		return $out;
	}

	public function option_akun($d=""){
		$value = isset($d['value'])?$d['value']:'';
		$tipe = isset($d['tipe'])?$d['tipe']:'';
		$kode_awal = isset($d['kode_awal'])?$d['kode_awal']:'';
		$out = '';
		
		$this->db->flush_cache();
		$this->db->from('akun');
		$this->db->order_by('kode_akun');

		if($tipe != '')
			$this->db->where('tipe', $tipe);

		// kode awal akun 1=pemasukan, 2=pengeluaran
		// untuk kebutuhan edit
		if($kode_awal != '')
			$this->db->like('kode_akun', $kode_awal, 'after');
		
		$res = $this->db->get();
		
		$out = '<option value="">-- Pilih --</option>';
		foreach($res->result() as $r){
			$out .= '<option value="'.$r->kode_akun.'" '.($r->kode_akun==$value?'selected="selected"':'').'>'.$r->deskripsi.'</option>';
		}
		
		return $out;
	}

	public function tree_akun($d=""){
		$this->db->flush_cache();

		$this->db->from('akun');
		
		$this->db->order_by('kode_akun', 'ASC');
		
		$query = $this->db->get();
		
		foreach($query->result() as $r){
			$data[$r->parent][] = $r;
		}
		
		$menu = $this->create_tree($data);

		//print_r($menu);
		return $menu;
	}

	/* untuk kebutuhan tree grid */
	public function create_tree($data, $parent = 0 ){
		if(isset($data[$parent])){
			$result = array();
			foreach($data[$parent] as $v){
				$node = array();
				$node['id'] = $v->kode_akun;
				$node['text'] = $v->deskripsi;
				$node['iconCls'] = $v->tipe == 'H' ? 'icon-akun_header' : '';
				$node['children'] = $this->create_tree($data, $v->kode_akun);
								
				if($node['children']){
					$node['state'] = 'opened';
				}
				
				array_push($result, $node);
			}
			return $result;
			
		}else{
			return false;
		}
	}

	public function option_otoritas($d=""){
		$value = isset($d['value'])?$d['value']:'';
		
		$this->db->flush_cache();
		$this->db->select('*');
		$this->db->from('sys_user_level');
		
		$this->db->order_by('level_name', 'ASC');
		
		$res = $this->db->get();
		
		$out = '<option value="">-- Pilih --</option>';
		foreach($res->result() as $r){
			$out .= '<option value="'.$r->user_level_id.'" '.($r->user_level_id==$value?'selected="selected"':'').'>'.$r->level_name.'</option>';
		}
		
		return $out;
	}
	
}

?>